#include "hal.h"
#include "dbg_tool.h"

#define TEST_COM HAL_COM1

static void on_com_rx_ready(const U8 port_num)
{
    for(;;)
    {
        U8 rx_buf[32];
        U32 len = hal_uart_read(port_num, rx_buf, sizeof(rx_buf));
        if(len == 0) break;
        trace_buffer(rx_buf, len);            
    }
}

/**
 *@brief main function
 */
int main(void)
{
    /* Initiate the HAL libraries */
    hal_init();

    /* default 115200,8,n,1     */
    const uart_cfg_t cfg = 
    {
        .baudrate = HAL_BAUDRATE_115200,
        .databit = HAL_UART_DATABIT_8,
        .stopbit = HAL_UART_STOPBIT_1,
        .parity = HAL_UART_PARITY_NONE
    };
    hal_uart_init(TEST_COM, &cfg);
    hal_uart_reg_rx_ready(TEST_COM, on_com_rx_ready);
    
    while (1)
    {
        /* toggle LED1 */
        hal_led_toggle(HAL_LED0);
        hal_delay_ms(500);
        hal_uart_write(TEST_COM, "123\r\n", 5);
    }
}
